$(function() {
	new Vue({
		el: '#addHomeMenu',
		data: {
			fullscreenLoading: false,
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			fileList:[],
			carouseImgUrl:[],
			form: {
				showOrder:"",
				logo:"",
				name:"",
			},
			homeMenuTable:[],
			dialogEditFormVisible:false,
			formLabelWidth:"120px",
			editFrom:{
				name:"",
				logo:"",
				showOrder:"",
				id:"",
			}
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../addHomeMenu/index.html";
			parent.vueData.activeIndex = "3";
			parent.vueData.getLeftNav("3")
			parent.vueData.$data.categoryIndex = 5;
			this.getAllHomeMenu();
		},
		methods: {
			//删除
			beforeRemove(file, fileList) {
				let index = this.fileList.findIndex(fileItem => {
					return fileItem.url === file.url
				})
				this.fileList.splice(index, 1)
				return true;
			},
			//上传
			handleImgSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.form.logo = response.result
					this.fileList.push({
						name: "logo",
						url: response.result,
					})
				}
			},
			handleImgExceed(files, fileList) {
				this.$message.warning("logo一次只能上传一张图片")
			},
			
			beforeCourseRemove(file, fileList) {
				  let index = this.carouseImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.carouseImgUrl.splice(index, 1)
				return true;
			},
			handleCoverExceed(files, fileList) {
				this.$message.warning("logo只能上传一张图片")
			},
			handleCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.editFrom.logo  = response.result
					this.carouseImgUrl.push({
						name: "logo",
			            url: response.result,
					})
					
				}
			},
			commitHomeMenu() {
				if(stringEmpty(this.form.name)){
					this.$message.error("名称不能为空")
					return;
				}
				if(this.fileList.length<=0){
					this.$message.error("logo不能为空")
					return;
				}
				if(stringEmpty(this.form.showOrder)){
					this.$message.error("权重不能为空")
					return;
				}
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.insertHomepageLogo,
					method: 'post',
					data: {
						name:this.form.name,
						logo:this.form.logo,
						showOrder:this.form.showOrder
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.form = {}
						 this.fileList = []
						  this.getAllHomeMenu();
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			
			//编辑上传
			configEditUpload(){
				if(stringEmpty(this.editFrom.name)){
					this.$message.error("名称不能为空")
					return;
				}
				if(this.carouseImgUrl.length<=0){
					this.$message.error("logo不能为空")
					return;
				}
				if(stringEmpty(this.editFrom.showOrder)){
					this.$message.error("权重不能为空")
					return;
				}
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.updateHomepageLogo,
					method: 'post',
					data: {
						id : this.editFrom.id,
						name:this.editFrom.name,
						logo:this.editFrom.logo,
						showOrder:this.editFrom.showOrder
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						  this.dialogEditFormVisible = false
						  this.editFrom = {}
						  this.carouseImgUrl = []
						  this.getAllHomeMenu();
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//删除
			deleteHomeMenuRow(val) {
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.deleteHomepageLogo,
					method: 'post',
					data: {
						id:val.id
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						  this.getAllHomeMenu();
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//编辑
			editHomeMenuRow(val){
                  this.dialogEditFormVisible = true;
                  this.carouseImgUrl= [];
                  this.carouseImgUrl.push({
                  	name: "logo",
                    url: val.logo,
                  })
                  this.editFrom.name = val.name
                  this.editFrom.showOrder = val.showOrder
				  this.editFrom.id = val.id
				  this.editFrom.logo = val.logo
			},
			getAllHomeMenu(){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.allHomepageLogo,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.homeMenuTable = response.data.result
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			}
		}
	})
})
